﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using IWshRuntimeLibrary;
using System.Reflection;

namespace E_mail
{
    internal static class SystemMenu
    {
        #region Var

        public static event EventHandler Added = delegate { };

        public static event EventHandler Removed = delegate { };

        #endregion

        #region Wywołanie zdarzeń dodania i usuniecia do menu systemowego

        public static void OnAdded()
        {
            Added(null, EventArgs.Empty);
        }

        public static void OnRemove()
        {
            Removed(null, EventArgs.Empty);
        }

        #endregion

        public static void Add()
        {
            try
            {
                var wshShellClass = new IWshShell_Class();
                string path = Environment.GetFolderPath(Environment.SpecialFolder.SendTo);

                var myShortcut = (IWshShortcut) wshShellClass.CreateShortcut(path + "\\Email.lnk");

                myShortcut.TargetPath = Assembly.GetExecutingAssembly().Location;
                myShortcut.IconLocation =
                    @"mail.ico";
                myShortcut.Description = "Uruchom email sender";
                myShortcut.Save();
            }
            catch (Exception exception)
            {
                Errors.NewException = exception;
                return;
            }
            OnAdded();
        }

        public static void Remove()
        {
            try
            {
                string path = Environment.GetFolderPath(Environment.SpecialFolder.SendTo);
                System.IO.File.Delete(path + "\\Email.lnk");
            }
            catch (Exception exception)
            {
                MessageBox.Show(exception.ToString());
                return;
            }
            if (Removed != null)
            {
                OnRemove();
            }
        }

        public static bool IsSet()
        {
            string path = Environment.GetFolderPath(Environment.SpecialFolder.SendTo) + "\\Email.lnk";
            return System.IO.File.Exists(path);
        }
    }
}